//
//  ChoiceLoginViewController.m
//  Model
//
//  Created by apple on 2018/7/9.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ChoiceLoginViewController.h"
#import "LoginViewController.h"
#import <UMSocialCore/UMSocialCore.h>
@interface ChoiceLoginViewController ()

@end

@implementation ChoiceLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creationView];
    
    // Do any additional setup after loading the view.
}
- (UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleLightContent;
    
    
}
- (void)creationView{
    
    UIImageView *beiJingImgView = [[UIImageView alloc] init];
    UIImage *beiJingImg = [UIImage imageNamed:@"bg_head_logo"];
    [self.view addSubview:beiJingImgView];
    [beiJingImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.offset(beiJingImg.size.height);
        
    }];
    beiJingImgView.image =beiJingImg;
    
    
//    UIImageView *logoImgView = [[UIImageView alloc] init];
//    [self.view addSubview:logoImgView];
//    [logoImgView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.view).with.offset(133);
//        make.centerX.equalTo(self.view);
//        make.width.offset(122);
//        make.height.offset(122);
//
//    }];
////    logoImgView.backgroundColor = [UIColor yellowColor];
//    logoImgView.image = [UIImage imageNamed:@"logo_z_y"];
    
    UIButton *weiXinBtn = [[UIButton alloc] init];
    [self.view addSubview:weiXinBtn];
    [weiXinBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(38);
        make.right.equalTo(self.view).with.offset(-38);
        make.top.equalTo(beiJingImgView.mas_bottom).with.offset(75);
        make.height.offset(44);
    }];
    [weiXinBtn setBackgroundImage:[UIImage imageNamed:@"b_bg1"] forState:UIControlStateNormal];

    
    [weiXinBtn setTitle:@"微信登录" forState:UIControlStateNormal];
    [weiXinBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    weiXinBtn.titleLabel.font = [UIFont systemFontOfSize:17];

    [weiXinBtn addTarget:self action:@selector(weiXinLogin) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIButton *phoneBtn = [[UIButton alloc] init];
    [self.view addSubview:phoneBtn];
    [phoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(38);
        make.right.equalTo(self.view).with.offset(-38);
        make.top.equalTo(weiXinBtn.mas_bottom).with.offset(34);
        make.height.offset(44);
    }];
    
    [phoneBtn setBackgroundImage:[UIImage imageNamed:@"b_bg1"] forState:UIControlStateNormal];
    [phoneBtn setTitle:@"账号登录" forState:UIControlStateNormal];

    [phoneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    phoneBtn.titleLabel.font = [UIFont systemFontOfSize:17];

    [phoneBtn addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *xieYiLab = [[UILabel alloc] init];
    [self.view addSubview:xieYiLab];
    [xieYiLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).with.offset(-20);
    }];
    xieYiLab.font = [UIFont systemFontOfSize:12];
    xieYiLab.textColor = getColor(@"b8b8b8");
    
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"*注册或登录则代表您同意《蜜桃用户协议》"];
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:12]
     
                          range:NSMakeRange(12, 8)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:getColor(@"ff40c6")
     
                          range:NSMakeRange(12, 8)];
    
    xieYiLab.attributedText = AttributedStr;
    
    
    
    
    
}
- (void)loginClick{
    LoginViewController *vc = [[LoginViewController alloc] init];
    
    [self presentViewController:vc animated:YES completion:^{
        
    }];
}
#pragma mark ------------   微信登录
- (void)weiXinLogin{
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    NSString *wxOpenId = [PublicFunction getConfigWithKey:@"wxOpenId"];
    [productParams setValue:wxOpenId forKey:@"wxOpenId"];
    NSLog(@"%@", productParams);
    [[MNetworkHandler sharedInstance]conURL:WxLogin networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        if ([[returnData objectForKey:@"Status"]intValue] == 200) {
            [PublicFunction saveConfig:returnData[@"Data"] forKey:@"user_id"];
            NSLog(@"%@", [PublicFunction getConfigWithKey:@"user_id"]);
            
            NSString *str =[NSString stringWithFormat:@"%@",[PublicFunction getConfigWithKey:@"user_id"]];
            
            NSString *encryStr = [str aci_encryptWithAES];
            
            [PublicFunction saveConfig:encryStr forKey:@"token"];
            NSLog(@"%@", [PublicFunction getConfigWithKey:@"token"]);
            
            MyTabBarController *barController = [[MyTabBarController alloc] init];
            barController.view.backgroundColor = getColor(@"ffffff");
            
            UINavigationBar *bar = [UINavigationBar appearance];
            //    if (KIsiPhoneX) {
            [bar setBackgroundImage:[UIImage imageNamed:@"bg_top"] forBarMetrics:UIBarMetricsDefault];
            [bar setShadowImage:nil];
            bar.translucent = NO;
            
            bar.barStyle = UIBarStyleBlackTranslucent;
            //设置显示的颜色
            bar.barTintColor = getColor(@"fe45cf");
            //设置字体颜色
            bar.tintColor = [UIColor whiteColor];
            [bar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
            
            //    [self presentViewController:barController animated:NO completion:nil];
            [self presentViewController:barController animated:NO completion:^{
                
            }];
            
        }else{
            [self weiXinClick];
            
        }
        
        
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
    }];
    
    
    
}
- (void)weiXinClick{
    
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {

//            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请先安装微信客户端" preferredStyle:UIAlertControllerStyleAlert];
//            UIAlertAction *actionConfirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
//            [alert addAction:actionConfirm];
//            [self presentViewController:alert animated:YES completion:nil];
            
        } else {
            UMSocialUserInfoResponse *resp = result;

            // 授权信息
            NSLog(@"Wechat uid: %@", resp.uid);
            NSLog(@"Wechat openid: %@", resp.openid);
            NSLog(@"Wechat unionid: %@", resp.unionId);
            NSLog(@"Wechat accessToken: %@", resp.accessToken);
            NSLog(@"Wechat refreshToken: %@", resp.refreshToken);
            NSLog(@"Wechat expiration: %@", resp.expiration);

            // 用户信息
            NSLog(@"Wechat name: %@", resp.name);
            NSLog(@"Wechat iconurl: %@", resp.iconurl);
            NSLog(@"Wechat gender: %@", resp.unionGender);

            // 第三方平台SDK源数据
            NSLog(@"Wechat originalResponse: %@", resp.originalResponse);
            NSString *sex=[NSString stringWithFormat:@"%ld", [[resp.originalResponse objectForKey:@"sex"] integerValue]];
            
            [PublicFunction saveConfig:resp.name forKey:@"wxNickName"];
            NSLog(@"%@", [PublicFunction getConfigWithKey:@"wxNickName"]);
            
            [PublicFunction saveConfig:resp.openid forKey:@"wxOpenId"];
            NSLog(@"%@", [PublicFunction getConfigWithKey:@"wxOpenId"]);
            
            [PublicFunction saveConfig:resp.unionId forKey:@"wxUnionId"];
            NSLog(@"%@", [PublicFunction getConfigWithKey:@"wxUnionId"]);
            
            [PublicFunction saveConfig:resp.iconurl forKey:@"wxHeadImg"];
            NSLog(@"%@", [PublicFunction getConfigWithKey:@"wxHeadImg"]);
            
            [PublicFunction saveConfig:sex forKey:@"wxGender"];
            NSLog(@"%@", [PublicFunction getConfigWithKey:@"wxGender"]);
            
            [self weiXinUrl];
            
            
        }
    }];
    
}
- (void)weiXinUrl{
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    NSString *wxOpenId = [PublicFunction getConfigWithKey:@"wxOpenId"];
    //    NSString *str = @"917b0ddccade4345b1b066fdc4971a14";
    NSString *wxUnionId = [PublicFunction getConfigWithKey:@"wxUnionId"];
    
    NSString *wxNickName = [PublicFunction getConfigWithKey:@"wxNickName"];
    NSString *wxHeadImg = [PublicFunction getConfigWithKey:@"wxHeadImg"];
    NSString *wxGender = [PublicFunction getConfigWithKey:@"wxGender"];
    
    
    [productParams setValue:wxUnionId forKey:@"wxUnionId"];
    [productParams setValue:wxOpenId forKey:@"wxOpenId"];
    [productParams setValue:wxNickName forKey:@"wxNickName"];
    [productParams setValue:wxHeadImg forKey:@"wxHeadImg"];
    [productParams setValue:wxGender forKey:@"wxGender"];
    
    [[MNetworkHandler sharedInstance]conURL:WxRegist networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        if ([[returnData objectForKey:@"Status"]intValue] == 200) {
          
            
            [PublicFunction saveConfig:returnData[@"Data"] forKey:@"user_id"];
            NSLog(@"%@", [PublicFunction getConfigWithKey:@"user_id"]);
            NSString *str =[NSString stringWithFormat:@"%@",[PublicFunction getConfigWithKey:@"user_id"]];
            
            NSString *encryStr = [str aci_encryptWithAES];
            
            [PublicFunction saveConfig:encryStr forKey:@"token"];
            NSLog(@"%@", [PublicFunction getConfigWithKey:@"token"]);
            MyTabBarController *barController = [[MyTabBarController alloc] init];
            barController.view.backgroundColor = getColor(@"ffffff");
            
            UINavigationBar *bar = [UINavigationBar appearance];
            //    if (KIsiPhoneX) {
            [bar setBackgroundImage:[UIImage imageNamed:@"bg_top"] forBarMetrics:UIBarMetricsDefault];
            [bar setShadowImage:nil];
            bar.translucent = NO;
            
            bar.barStyle = UIBarStyleBlackTranslucent;
            //设置显示的颜色
            bar.barTintColor = getColor(@"fe45cf");
            //设置字体颜色
            bar.tintColor = [UIColor whiteColor];
            [bar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
            
            //    [self presentViewController:barController animated:NO completion:nil];
            [self presentViewController:barController animated:NO completion:^{
                
            }];
            
        }else{
            [PublicFunction showAlert:nil message:@"登录失败" duration:10.0f];
        }
            
       
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
    }];
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
